home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.006 / xemacs-1 / lib / xemacs-19.13 / info / lispref.info-11 < prev    next >
Encoding:
GNU Info File  |  1995-09-01  |  49.5 KB  |  1,172 lines

  1. This is Info file ../../info/lispref.info, produced by Makeinfo-1.63
  2. from the input file lispref.texi.
  3.  
  4.    Edition History:
  5.  
  6.    GNU Emacs Lisp Reference Manual Second Edition (v2.01), May 1993 GNU
  7. Emacs Lisp Reference Manual Further Revised (v2.02), August 1993 Lucid
  8. Emacs Lisp Reference Manual (for 19.10) First Edition, March 1994
  9. XEmacs Lisp Programmer's Manual (for 19.12) Second Edition, April 1995
  10. GNU Emacs Lisp Reference Manual v2.4, June 1995 XEmacs Lisp
  11. Programmer's Manual (for 19.13) Third Edition, July 1995
  12.  
  13.    Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995 Free Software
  14. Foundation, Inc.  Copyright (C) 1994, 1995 Sun Microsystems, Inc.
  15. Copyright (C) 1995 Amdahl Corporation.  Copyright (C) 1995 Ben Wing.
  16.  
  17.    Permission is granted to make and distribute verbatim copies of this
  18. manual provided the copyright notice and this permission notice are
  19. preserved on all copies.
  20.  
  21.    Permission is granted to copy and distribute modified versions of
  22. this manual under the conditions for verbatim copying, provided that the
  23. entire resulting derived work is distributed under the terms of a
  24. permission notice identical to this one.
  25.  
  26.    Permission is granted to copy and distribute translations of this
  27. manual into another language, under the above conditions for modified
  28. versions, except that this permission notice may be stated in a
  29. translation approved by the Foundation.
  30.  
  31.    Permission is granted to copy and distribute modified versions of
  32. this manual under the conditions for verbatim copying, provided also
  33. that the section entitled "GNU General Public License" is included
  34. exactly as in the original, and provided that the entire resulting
  35. derived work is distributed under the terms of a permission notice
  36. identical to this one.
  37.  
  38.    Permission is granted to copy and distribute translations of this
  39. manual into another language, under the above conditions for modified
  40. versions, except that the section entitled "GNU General Public License"
  41. may be included in a translation approved by the Free Software
  42. Foundation instead of in the original English.
  43.  
  44. 
  45. File: lispref.info,  Node: Compilation Functions,  Next: Docs and Compilation,  Prev: Speed of Byte-Code,  Up: Byte Compilation
  46.  
  47. The Compilation Functions
  48. =========================
  49.  
  50.    You can byte-compile an individual function or macro definition with
  51. the `byte-compile' function.  You can compile a whole file with
  52. `byte-compile-file', or several files with `byte-recompile-directory'
  53. or `batch-byte-compile'.
  54.  
  55.    When you run the byte compiler, you may get warnings in a buffer
  56. called `*Compile-Log*'.  These report things in your program that
  57. suggest a problem but are not necessarily erroneous.
  58.  
  59.    Be careful when byte-compiling code that uses macros.  Macro calls
  60. are expanded when they are compiled, so the macros must already be
  61. defined for proper compilation.  For more details, see *Note Compiling
  62. Macros::.
  63.  
  64.    Normally, compiling a file does not evaluate the file's contents or
  65. load the file.  But it does execute any `require' calls at top level in
  66. the file.  One way to ensure that necessary macro definitions are
  67. available during compilation is to require the file that defines them
  68. (*note Named Features::.).  To avoid loading the macro definition files
  69. when someone *runs* the compiled program, write `eval-when-compile'
  70. around the `require' calls (*note Eval During Compile::.).
  71.  
  72.  - Function: byte-compile SYMBOL
  73.      This function byte-compiles the function definition of SYMBOL,
  74.      replacing the previous definition with the compiled one.  The
  75.      function definition of SYMBOL must be the actual code for the
  76.      function; i.e., the compiler does not follow indirection to
  77.      another symbol.  `byte-compile' returns the new, compiled
  78.      definition of SYMBOL.
  79.  
  80.      If SYMBOL's definition is a byte-code function object,
  81.      `byte-compile' does nothing and returns `nil'.  Lisp records only
  82.      one function definition for any symbol, and if that is already
  83.      compiled, non-compiled code is not available anywhere.  So there
  84.      is no way to "compile the same definition again."
  85.  
  86.           (defun factorial (integer)
  87.             "Compute factorial of INTEGER."
  88.             (if (= 1 integer) 1
  89.               (* integer (factorial (1- integer)))))
  90.           => factorial
  91.           
  92.           (byte-compile 'factorial)
  93.                =>
  94.           #<byte-code (integer)
  95.            "┴U½é┴ç┬S!_ç"
  96.            [integer 1 factorial]
  97.            3 "Compute factorial of INTEGER.">
  98.  
  99.      The result is a byte-code function object.  The string it contains
  100.      is the actual byte-code; each character in it is an instruction or
  101.      an operand of an instruction.  The vector contains all the
  102.      constants, variable names and function names used by the function,
  103.      except for certain primitives that are coded as special
  104.      instructions.
  105.  
  106.  - Command: compile-defun &optional ARG
  107.      This command reads the defun containing point, compiles it, and
  108.      evaluates the result.  If you use this on a defun that is actually
  109.      a function definition, the effect is to install a compiled version
  110.      of that function.
  111.  
  112.      If ARG is non-`nil', the result is inserted in the current buffer
  113.      after the form; otherwise, it is printed in the minibuffer.
  114.  
  115.  - Command: byte-compile-file FILENAME &optional LOAD
  116.      This function compiles a file of Lisp code named FILENAME into a
  117.      file of byte-code.  The output file's name is made by appending
  118.      `c' to the end of FILENAME.
  119.  
  120.      If `load' is non-`nil', the file is loaded after having been
  121.      compiled.
  122.  
  123.      Compilation works by reading the input file one form at a time.
  124.      If it is a definition of a function or macro, the compiled
  125.      function or macro definition is written out.  Other forms are
  126.      batched together, then each batch is compiled, and written so that
  127.      its compiled code will be executed when the file is read.  All
  128.      comments are discarded when the input file is read.
  129.  
  130.      This command returns `t'.  When called interactively, it prompts
  131.      for the file name.
  132.  
  133.           % ls -l push*
  134.           -rw-r--r--  1 lewis     791 Oct  5 20:31 push.el
  135.           
  136.           (byte-compile-file "~/emacs/push.el")
  137.                => t
  138.           
  139.           % ls -l push*
  140.           -rw-r--r--  1 lewis     791 Oct  5 20:31 push.el
  141.           -rw-rw-rw-  1 lewis     638 Oct  8 20:25 push.elc
  142.  
  143.  - Command: byte-recompile-directory DIRECTORY &optional FLAG
  144.      This function recompiles every `.el' file in DIRECTORY that needs
  145.      recompilation.  A file needs recompilation if a `.elc' file exists
  146.      but is older than the `.el' file.
  147.  
  148.      When a `.el' file has no corresponding `.elc' file, then FLAG says
  149.      what to do.  If it is `nil', these files are ignored.  If it is
  150.      non-`nil', the user is asked whether to compile each such file.
  151.  
  152.      The returned value of this command is unpredictable.
  153.  
  154.  - Function: batch-byte-compile
  155.      This function runs `byte-compile-file' on files specified on the
  156.      command line.  This function must be used only in a batch
  157.      execution of Emacs, as it kills Emacs on completion.  An error in
  158.      one file does not prevent processing of subsequent files.  (The
  159.      file that gets the error will not, of course, produce any compiled
  160.      code.)
  161.  
  162.           % emacs -batch -f batch-byte-compile *.el
  163.  
  164.  - Function: batch-byte-recompile-directory
  165.      This function is similar to `batch-byte-compile' but runs the
  166.      command `byte-recompile-directory' on the files remaining on the
  167.      command line.
  168.  
  169.  - Variable: byte-recompile-directory-ignore-errors-p
  170.      If non-`nil', this specifies that `byte-recompile-directory' will
  171.      continue compiling even when an error occurs in a file.  This is
  172.      normally `nil', but is bound to `t' by
  173.      `batch-byte-recompile-directory'.
  174.  
  175.  - Function: byte-code CODE-STRING DATA-VECTOR MAX-STACK
  176.      This function actually interprets byte-code.  A byte-compiled
  177.      function is actually defined with a body that calls `byte-code'.
  178.      Don't call this function yourself.  Only the byte compiler knows
  179.      how to generate valid calls to this function.
  180.  
  181.      In newer Emacs versions (19 and up), byte-code is usually executed
  182.      as part of a byte-code function object, and only rarely due to an
  183.      explicit call to `byte-code'.
  184.  
  185. 
  186. File: lispref.info,  Node: Docs and Compilation,  Next: Dynamic Loading,  Prev: Compilation Functions,  Up: Byte Compilation
  187.  
  188. Documentation Strings and Compilation
  189. =====================================
  190.  
  191.    Functions and variables loaded from a byte-compiled file access their
  192. documentation strings dynamically from the file whenever needed.  This
  193. saves space within Emacs, and makes loading faster because the
  194. documentation strings themselves need not be processed while loading the
  195. file.  Actual access to the documentation strings becomes slower as a
  196. result, but this normally is not enough to bother users.
  197.  
  198.    Dynamic access to documentation strings does have drawbacks:
  199.  
  200.    * If you delete or move the compiled file after loading it, Emacs
  201.      can no longer access the documentation strings for the functions
  202.      and variables in the file.
  203.  
  204.    * If you alter the compiled file (such as by compiling a new
  205.      version), then further access to documentation strings in this
  206.      file will give nonsense results.
  207.  
  208.    If your site installs Emacs following the usual procedures, these
  209. problems will never normally occur.  Installing a new version uses a new
  210. directory with a different name; as long as the old version remains
  211. installed, its files will remain unmodified in the places where they are
  212. expected to be.
  213.  
  214.    However, if you have built Emacs yourself and use it from the
  215. directory where you built it, you will experience this problem
  216. occasionally if you edit and recompile Lisp files.  When it happens, you
  217. can cure the problem by reloading the file after recompiling it.
  218.  
  219.    Byte-compiled files made with Emacs 19.29 will not load into older
  220. versions because the older versions don't support this feature.  You can
  221. turn off this feature by setting `byte-compile-dynamic-docstrings' to
  222. `nil'.  Once this is done, you can compile files that will load into
  223. older Emacs versions.  You can do this globally, or for one source file
  224. by specifying a file-local binding for the variable.  Here's one way to
  225. do that:
  226.  
  227.      -*-byte-compile-dynamic-docstrings: nil;-*-
  228.  
  229.  - Variable: byte-compile-dynamic-docstrings
  230.      If this is non-`nil', the byte compiler generates compiled files
  231.      that are set up for dynamic loading of documentation strings.
  232.  
  233.    The dynamic documentation string feature writes compiled files that
  234. use a special Lisp reader construct, `#@COUNT'.  This construct skips
  235. the next COUNT characters.  It also uses the `#$' construct, which
  236. stands for "the name of this file, as a string."  It is best not to use
  237. these constructs in Lisp source files.
  238.  
  239. 
  240. File: lispref.info,  Node: Dynamic Loading,  Next: Eval During Compile,  Prev: Docs and Compilation,  Up: Byte Compilation
  241.  
  242. Dynamic Loading of Individual Functions
  243. =======================================
  244.  
  245.    When you compile a file, you can optionally enable the "dynamic
  246. function loading" feature (also known as "lazy loading").  With dynamic
  247. function loading, loading the file doesn't fully read the function
  248. definitions in the file.  Instead, each function definition contains a
  249. place-holder which refers to the file.  The first time each function is
  250. called, it reads the full definition from the file, to replace the
  251. place-holder.
  252.  
  253.    The advantage of dynamic function loading is that loading the file
  254. becomes much faster.  This is a good thing for a file which contains
  255. many separate commands, provided that using one of them does not imply
  256. you will soon (or ever) use the rest.  A specialized mode which provides
  257. many keyboard commands often has that usage pattern: a user may invoke
  258. the mode, but use only a few of the commands it provides.
  259.  
  260.    The dynamic loading feature has certain disadvantages:
  261.  
  262.    * If you delete or move the compiled file after loading it, Emacs
  263.      can no longer load the remaining function definitions not already
  264.      loaded.
  265.  
  266.    * If you alter the compiled file (such as by compiling a new
  267.      version), then trying to load any function not already loaded will
  268.      get nonsense results.
  269.  
  270.    If you compile a new version of the file, the best thing to do is
  271. immediately load the new compiled file.  That will prevent any future
  272. problems.
  273.  
  274.    The byte compiler uses the dynamic function loading feature if the
  275. variable `byte-compile-dynamic' is non-`nil' at compilation time.  Do
  276. not set this variable globally, since dynamic loading is desirable only
  277. for certain files.  Instead, enable the feature for specific source
  278. files with file-local variable bindings, like this:
  279.  
  280.      -*-byte-compile-dynamic: t;-*-
  281.  
  282.  - Variable: byte-compile-dynamic
  283.      If this is non-`nil', the byte compiler generates compiled files
  284.      that are set up for dynamic function loading.
  285.  
  286.  - Function: fetch-bytecode FUNCTION
  287.      This immediately finishes loading the definition of FUNCTION from
  288.      its byte-compiled file, if it is not fully loaded already.  The
  289.      argument FUNCTION may be a byte-code function object or a function
  290.      name.
  291.  
  292. 
  293. File: lispref.info,  Node: Eval During Compile,  Next: Byte-Code Objects,  Prev: Dynamic Loading,  Up: Byte Compilation
  294.  
  295. Evaluation During Compilation
  296. =============================
  297.  
  298.    These features permit you to write code to be evaluated during
  299. compilation of a program.
  300.  
  301.  - Special Form: eval-and-compile BODY
  302.      This form marks BODY to be evaluated both when you compile the
  303.      containing code and when you run it (whether compiled or not).
  304.  
  305.      You can get a similar result by putting BODY in a separate file
  306.      and referring to that file with `require'.  Using `require' is
  307.      preferable if there is a substantial amount of code to be executed
  308.      in this way.
  309.  
  310.  - Special Form: eval-when-compile BODY
  311.      This form marks BODY to be evaluated at compile time and not when
  312.      the compiled program is loaded.  The result of evaluation by the
  313.      compiler becomes a constant which appears in the compiled program.
  314.      When the program is interpreted, not compiled at all, BODY is
  315.      evaluated normally.
  316.  
  317.      At top level, this is analogous to the Common Lisp idiom
  318.      `(eval-when (compile eval) ...)'.  Elsewhere, the Common Lisp `#.'
  319.      reader macro (but not when interpreting) is closer to what
  320.      `eval-when-compile' does.
  321.  
  322. 
  323. File: lispref.info,  Node: Byte-Code Objects,  Next: Disassembly,  Prev: Eval During Compile,  Up: Byte Compilation
  324.  
  325. Byte-Code Function Objects
  326. ==========================
  327.  
  328.    Byte-compiled functions have a special data type: they are
  329. "byte-code function objects".
  330.  
  331.    Internally, a byte-code function object is much like a vector;
  332. however, the evaluator handles this data type specially when it appears
  333. as a function to be called.  The printed representation for a byte-code
  334. function object begins with `#<byte-code' and ends with `>'.
  335.  
  336.    In Emacs version 18, there was no byte-code function object data
  337. type; compiled functions used the function `byte-code' to run the byte
  338. code.
  339.  
  340.    A byte-code function object must have at least four elements; there
  341. is no maximum number, but only the first six elements are actually used.
  342. They are:
  343.  
  344. ARGLIST
  345.      The list of argument symbols.
  346.  
  347. BYTE-CODE
  348.      The string containing the byte-code instructions.
  349.  
  350. CONSTANTS
  351.      The vector of Lisp objects referenced by the byte code.  These
  352.      include symbols used as function names and variable names.
  353.  
  354. STACKSIZE
  355.      The maximum stack size this function needs.
  356.  
  357. DOCSTRING
  358.      The documentation string (if any); otherwise, `nil'.  The value may
  359.      be a number or a list, in case the documentation string is stored
  360.      in a file.  Use the function `documentation' to get the real
  361.      documentation string (*note Accessing Documentation::.).
  362.  
  363. INTERACTIVE
  364.      The interactive spec (if any).  This can be a string or a Lisp
  365.      expression.  It is `nil' for a function that isn't interactive.
  366.  
  367.    Here's an example of a byte-code function object, in printed
  368. representation.  It is the definition of the command `backward-sexp'.
  369.  
  370.      #[(&optional arg)
  371.        "^H\204^F^@\301^P\302^H[!\207"
  372.        [arg 1 forward-sexp]
  373.        2
  374.        254435
  375.        "p"]
  376.  
  377.    The primitive way to create a byte-code object is with
  378. `make-byte-code':
  379.  
  380.  - Function: make-byte-code &rest ELEMENTS
  381.      This function constructs and returns a byte-code function object
  382.      with ELEMENTS as its elements.
  383.  
  384.    You should not try to come up with the elements for a byte-code
  385. function yourself, because if they are inconsistent, Emacs may crash
  386. when you call the function.  Always leave it to the byte compiler to
  387. create these objects; it makes the elements consistent (we hope).
  388.  
  389.    You can access the elements of a byte-code object using `aref'; you
  390. can also use `vconcat' to create a vector with the same elements.
  391.  
  392. 
  393. File: lispref.info,  Node: Disassembly,  Prev: Byte-Code Objects,  Up: Byte Compilation
  394.  
  395. Disassembled Byte-Code
  396. ======================
  397.  
  398.    People do not write byte-code; that job is left to the byte compiler.
  399. But we provide a disassembler to satisfy a cat-like curiosity.  The
  400. disassembler converts the byte-compiled code into humanly readable form.
  401.  
  402.    The byte-code interpreter is implemented as a simple stack machine.
  403. It pushes values onto a stack of its own, then pops them off to use them
  404. in calculations whose results are themselves pushed back on the stack.
  405. When a byte-code function returns, it pops a value off the stack and
  406. returns it as the value of the function.
  407.  
  408.    In addition to the stack, byte-code functions can use, bind, and set
  409. ordinary Lisp variables, by transferring values between variables and
  410. the stack.
  411.  
  412.  - Command: disassemble OBJECT &optional STREAM
  413.      This function prints the disassembled code for OBJECT.  If STREAM
  414.      is supplied, then output goes there.  Otherwise, the disassembled
  415.      code is printed to the stream `standard-output'.  The argument
  416.      OBJECT can be a function name or a lambda expression.
  417.  
  418.      As a special exception, if this function is used interactively, it
  419.      outputs to a buffer named `*Disassemble*'.
  420.  
  421.    Here are two examples of using the `disassemble' function.  We have
  422. added explanatory comments to help you relate the byte-code to the Lisp
  423. source; these do not appear in the output of `disassemble'.  These
  424. examples show unoptimized byte-code.  Nowadays byte-code is usually
  425. optimized, but we did not want to rewrite these examples, since they
  426. still serve their purpose.
  427.  
  428.      (defun factorial (integer)
  429.        "Compute factorial of an integer."
  430.        (if (= 1 integer) 1
  431.          (* integer (factorial (1- integer)))))
  432.           => factorial
  433.      
  434.      (factorial 4)
  435.           => 24
  436.      
  437.      (disassemble 'factorial)
  438.           -| byte-code for factorial:
  439.       doc: Compute factorial of an integer.
  440.       args: (integer)
  441.      
  442.      0   constant 1              ; Push 1 onto stack.
  443.      
  444.      1   varref   integer        ; Get value of `integer'
  445.                                  ;   from the environment
  446.                                  ;   and push the value
  447.                                  ;   onto the stack.
  448.      
  449.      2   eqlsign                 ; Pop top two values off stack,
  450.                                  ;   compare them,
  451.                                  ;   and push result onto stack.
  452.      
  453.      3   goto-if-nil 10          ; Pop and test top of stack;
  454.                                  ;   if `nil', go to 10,
  455.                                  ;   else continue.
  456.      
  457.      6   constant 1              ; Push 1 onto top of stack.
  458.      
  459.      7   goto     17             ; Go to 17 (in this case, 1 will be
  460.                                  ;   returned by the function).
  461.      
  462.      10  constant *              ; Push symbol `*' onto stack.
  463.      
  464.      11  varref   integer        ; Push value of `integer' onto stack.
  465.      
  466.      12  constant factorial      ; Push `factorial' onto stack.
  467.      
  468.      13  varref   integer        ; Push value of `integer' onto stack.
  469.      
  470.      14  sub1                    ; Pop `integer', decrement value,
  471.                                  ;   push new value onto stack.
  472.      
  473.                                  ; Stack now contains:
  474.                                  ;   - decremented value of `integer'
  475.                                  ;   - `factorial'
  476.                                  ;   - value of `integer'
  477.                                  ;   - `*'
  478.      
  479.      15  call     1              ; Call function `factorial' using
  480.                                  ;   the first (i.e., the top) element
  481.                                  ;   of the stack as the argument;
  482.                                  ;   push returned value onto stack.
  483.      
  484.                                  ; Stack now contains:
  485.                                  ;   - result of recursive
  486.                                  ;        call to `factorial'
  487.                                  ;   - value of `integer'
  488.                                  ;   - `*'
  489.      
  490.      16  call     2              ; Using the first two
  491.                                  ;   (i.e., the top two)
  492.                                  ;   elements of the stack
  493.                                  ;   as arguments,
  494.                                  ;   call the function `*',
  495.                                  ;   pushing the result onto the stack.
  496.      
  497.      17  return                  ; Return the top element
  498.                                  ;   of the stack.
  499.           => nil
  500.  
  501.    The `silly-loop' function is somewhat more complex:
  502.  
  503.      (defun silly-loop (n)
  504.        "Return time before and after N iterations of a loop."
  505.        (let ((t1 (current-time-string)))
  506.          (while (> (setq n (1- n))
  507.                    0))
  508.          (list t1 (current-time-string))))
  509.           => silly-loop
  510.      
  511.      (disassemble 'silly-loop)
  512.           -| byte-code for silly-loop:
  513.       doc: Return time before and after N iterations of a loop.
  514.       args: (n)
  515.      
  516.      0   constant current-time-string  ; Push
  517.                                        ;   `current-time-string'
  518.                                        ;   onto top of stack.
  519.      
  520.      1   call     0              ; Call `current-time-string'
  521.                                  ;    with no argument,
  522.                                  ;    pushing result onto stack.
  523.      
  524.      2   varbind  t1             ; Pop stack and bind `t1'
  525.                                  ;   to popped value.
  526.      
  527.      3   varref   n              ; Get value of `n' from
  528.                                  ;   the environment and push
  529.                                  ;   the value onto the stack.
  530.      
  531.      4   sub1                    ; Subtract 1 from top of stack.
  532.      
  533.      5   dup                     ; Duplicate the top of the stack;
  534.                                  ;   i.e., copy the top of
  535.                                  ;   the stack and push the
  536.                                  ;   copy onto the stack.
  537.      
  538.      6   varset   n              ; Pop the top of the stack,
  539.                                  ;   and bind `n' to the value.
  540.      
  541.                                  ; In effect, the sequence `dup varset'
  542.                                  ;   copies the top of the stack
  543.                                  ;   into the value of `n'
  544.                                  ;   without popping it.
  545.      
  546.      7   constant 0              ; Push 0 onto stack.
  547.      
  548.      8   gtr                     ; Pop top two values off stack,
  549.                                  ;   test if N is greater than 0
  550.                                  ;   and push result onto stack.
  551.      
  552.      9   goto-if-nil-else-pop 17 ; Goto 17 if `n' <= 0
  553.                                  ;   (this exits the while loop).
  554.                                  ;   else pop top of stack
  555.                                  ;   and continue
  556.      
  557.      12  constant nil            ; Push `nil' onto stack
  558.                                  ;   (this is the body of the loop).
  559.      
  560.      13  discard                 ; Discard result of the body
  561.                                  ;   of the loop (a while loop
  562.                                  ;   is always evaluated for
  563.                                  ;   its side effects).
  564.      
  565.      14  goto     3              ; Jump back to beginning
  566.                                  ;   of while loop.
  567.      
  568.      17  discard                 ; Discard result of while loop
  569.                                  ;   by popping top of stack.
  570.                                  ;   This result is the value `nil' that
  571.                                  ;   was not popped by the goto at 9.
  572.      
  573.      18  varref   t1             ; Push value of `t1' onto stack.
  574.      
  575.      19  constant current-time-string  ; Push
  576.                                        ;   `current-time-string'
  577.                                        ;   onto top of stack.
  578.      
  579.      20  call     0              ; Call `current-time-string' again.
  580.      
  581.      21  list2                   ; Pop top two elements off stack,
  582.                                  ;   create a list of them,
  583.                                  ;   and push list onto stack.
  584.      
  585.      22  unbind   1              ; Unbind `t1' in local environment.
  586.      
  587.      23  return                  ; Return value of the top of stack.
  588.      
  589.           => nil
  590.  
  591. 
  592. File: lispref.info,  Node: Debugging,  Next: Read and Print,  Prev: Byte Compilation,  Up: Top
  593.  
  594. Debugging Lisp Programs
  595. ***********************
  596.  
  597.    There are three ways to investigate a problem in an Emacs Lisp
  598. program, depending on what you are doing with the program when the
  599. problem appears.
  600.  
  601.    * If the problem occurs when you run the program, you can use a Lisp
  602.      debugger (either the default debugger or Edebug) to investigate
  603.      what is happening during execution.
  604.  
  605.    * If the problem is syntactic, so that Lisp cannot even read the
  606.      program, you can use the XEmacs facilities for editing Lisp to
  607.      localize it.
  608.  
  609.    * If the problem occurs when trying to compile the program with the
  610.      byte compiler, you need to know how to examine the compiler's
  611.      input buffer.
  612.  
  613. * Menu:
  614.  
  615. * Debugger::            How the Emacs Lisp debugger is implemented.
  616. * Syntax Errors::       How to find syntax errors.
  617. * Compilation Errors::  How to find errors that show up in byte compilation.
  618. * Edebug::        A source-level Emacs Lisp debugger.
  619.  
  620.    Another useful debugging tool is the dribble file.  When a dribble
  621. file is open, XEmacs copies all keyboard input characters to that file.
  622. Afterward, you can examine the file to find out what input was used.
  623. *Note Terminal Input::.
  624.  
  625.    For debugging problems in terminal descriptions, the
  626. `open-termscript' function can be useful.  *Note Terminal Output::.
  627.  
  628. 
  629. File: lispref.info,  Node: Debugger,  Next: Syntax Errors,  Up: Debugging
  630.  
  631. The Lisp Debugger
  632. =================
  633.  
  634.    The "Lisp debugger" provides the ability to suspend evaluation of a
  635. form.  While evaluation is suspended (a state that is commonly known as
  636. a "break"), you may examine the run time stack, examine the values of
  637. local or global variables, or change those values.  Since a break is a
  638. recursive edit, all the usual editing facilities of XEmacs are
  639. available; you can even run programs that will enter the debugger
  640. recursively.  *Note Recursive Editing::.
  641.  
  642. * Menu:
  643.  
  644. * Error Debugging::       Entering the debugger when an error happens.
  645. * Infinite Loops::      Stopping and debugging a program that doesn't exit.
  646. * Function Debugging::    Entering it when a certain function is called.
  647. * Explicit Debug::        Entering it at a certain point in the program.
  648. * Using Debugger::        What the debugger does; what you see while in it.
  649. * Debugger Commands::     Commands used while in the debugger.
  650. * Invoking the Debugger:: How to call the function `debug'.
  651. * Internals of Debugger:: Subroutines of the debugger, and global variables.
  652.  
  653. 
  654. File: lispref.info,  Node: Error Debugging,  Next: Infinite Loops,  Up: Debugger
  655.  
  656. Entering the Debugger on an Error
  657. ---------------------------------
  658.  
  659.    The most important time to enter the debugger is when a Lisp error
  660. happens.  This allows you to investigate the immediate causes of the
  661. error.
  662.  
  663.    However, entry to the debugger is not a normal consequence of an
  664. error.  Many commands frequently get Lisp errors when invoked in
  665. inappropriate contexts (such as `C-f' at the end of the buffer) and
  666. during ordinary editing it would be very unpleasant to enter the
  667. debugger each time this happens.  If you want errors to enter the
  668. debugger, set the variable `debug-on-error' to non-`nil'.
  669.  
  670.  - User Option: debug-on-error
  671.      This variable determines whether the debugger is called when an
  672.      error is signaled and not handled.  If `debug-on-error' is `t', all
  673.      errors call the debugger.  If it is `nil', none call the debugger.
  674.  
  675.      The value can also be a list of error conditions that should call
  676.      the debugger.  For example, if you set it to the list
  677.      `(void-variable)', then only errors about a variable that has no
  678.      value invoke the debugger.
  679.  
  680.      When this variable is non-`nil', Emacs does not catch errors that
  681.      happen in process filter functions and sentinels.  Therefore, these
  682.      errors also can invoke the debugger.  *Note Processes::.
  683.  
  684.    To debug an error that happens during loading of the `.emacs' file,
  685. use the option `-debug-init', which binds `debug-on-error' to `t' while
  686. `.emacs' is loaded and inhibits use of `condition-case' to catch init
  687. file errors.
  688.  
  689.    If your `.emacs' file sets `debug-on-error', the effect may not last
  690. past the end of loading `.emacs'.  (This is an undesirable byproduct of
  691. the code that implements the `-debug-init' command line option.)  The
  692. best way to make `.emacs' set `debug-on-error' permanently is with
  693. `after-init-hook', like this:
  694.  
  695.      (add-hook 'after-init-hook
  696.                '(lambda () (setq debug-on-error t)))
  697.  
  698. 
  699. File: lispref.info,  Node: Infinite Loops,  Next: Function Debugging,  Prev: Error Debugging,  Up: Debugger
  700.  
  701. Debugging Infinite Loops
  702. ------------------------
  703.  
  704.    When a program loops infinitely and fails to return, your first
  705. problem is to stop the loop.  On most operating systems, you can do this
  706. with `C-g', which causes quit.
  707.  
  708.    Ordinary quitting gives no information about why the program was
  709. looping.  To get more information, you can set the variable
  710. `debug-on-quit' to non-`nil'.  Quitting with `C-g' is not considered an
  711. error, and `debug-on-error' has no effect on the handling of `C-g'.
  712. Likewise, `debug-on-quit' has no effect on errors.
  713.  
  714.    Once you have the debugger running in the middle of the infinite
  715. loop, you can proceed from the debugger using the stepping commands.
  716. If you step through the entire loop, you will probably get enough
  717. information to solve the problem.
  718.  
  719.  - User Option: debug-on-quit
  720.      This variable determines whether the debugger is called when `quit'
  721.      is signaled and not handled.  If `debug-on-quit' is non-`nil',
  722.      then the debugger is called whenever you quit (that is, type
  723.      `C-g').  If `debug-on-quit' is `nil', then the debugger is not
  724.      called when you quit.  *Note Quitting::.
  725.  
  726. 
  727. File: lispref.info,  Node: Function Debugging,  Next: Explicit Debug,  Prev: Infinite Loops,  Up: Debugger
  728.  
  729. Entering the Debugger on a Function Call
  730. ----------------------------------------
  731.  
  732.    To investigate a problem that happens in the middle of a program, one
  733. useful technique is to enter the debugger whenever a certain function is
  734. called.  You can do this to the function in which the problem occurs,
  735. and then step through the function, or you can do this to a function
  736. called shortly before the problem, step quickly over the call to that
  737. function, and then step through its caller.
  738.  
  739.  - Command: debug-on-entry FUNCTION-NAME
  740.      This function requests FUNCTION-NAME to invoke the debugger each
  741.      time it is called.  It works by inserting the form `(debug
  742.      'debug)' into the function definition as the first form.
  743.  
  744.      Any function defined as Lisp code may be set to break on entry,
  745.      regardless of whether it is interpreted code or compiled code.  If
  746.      the function is a command, it will enter the debugger when called
  747.      from Lisp and when called interactively (after the reading of the
  748.      arguments).  You can't debug primitive functions (i.e., those
  749.      written in C) this way.
  750.  
  751.      When `debug-on-entry' is called interactively, it prompts for
  752.      FUNCTION-NAME in the minibuffer.
  753.  
  754.      If the function is already set up to invoke the debugger on entry,
  755.      `debug-on-entry' does nothing.
  756.  
  757.      *Note:* if you redefine a function after using `debug-on-entry' on
  758.      it, the code to enter the debugger is lost.
  759.  
  760.      `debug-on-entry' returns FUNCTION-NAME.
  761.  
  762.           (defun fact (n)
  763.             (if (zerop n) 1
  764.                 (* n (fact (1- n)))))
  765.                => fact
  766.           (debug-on-entry 'fact)
  767.                => fact
  768.           (fact 3)
  769.           
  770.           ------ Buffer: *Backtrace* ------
  771.           Entering:
  772.           * fact(3)
  773.             eval-region(4870 4878 t)
  774.             byte-code("...")
  775.             eval-last-sexp(nil)
  776.             (let ...)
  777.             eval-insert-last-sexp(nil)
  778.           * call-interactively(eval-insert-last-sexp)
  779.           ------ Buffer: *Backtrace* ------
  780.           
  781.           (symbol-function 'fact)
  782.                => (lambda (n)
  783.                     (debug (quote debug))
  784.                     (if (zerop n) 1 (* n (fact (1- n)))))
  785.  
  786.  - Command: cancel-debug-on-entry FUNCTION-NAME
  787.      This function undoes the effect of `debug-on-entry' on
  788.      FUNCTION-NAME.  When called interactively, it prompts for
  789.      FUNCTION-NAME in the minibuffer.  If FUNCTION-NAME is `nil' or the
  790.      empty string, it cancels debugging for all functions.
  791.  
  792.      If `cancel-debug-on-entry' is called more than once on the same
  793.      function, the second call does nothing.  `cancel-debug-on-entry'
  794.      returns FUNCTION-NAME.
  795.  
  796. 
  797. File: lispref.info,  Node: Explicit Debug,  Next: Using Debugger,  Prev: Function Debugging,  Up: Debugger
  798.  
  799. Explicit Entry to the Debugger
  800. ------------------------------
  801.  
  802.    You can cause the debugger to be called at a certain point in your
  803. program by writing the expression `(debug)' at that point.  To do this,
  804. visit the source file, insert the text `(debug)' at the proper place,
  805. and type `C-M-x'.  Be sure to undo this insertion before you save the
  806. file!
  807.  
  808.    The place where you insert `(debug)' must be a place where an
  809. additional form can be evaluated and its value ignored.  (If the value
  810. of `(debug)' isn't ignored, it will alter the execution of the
  811. program!)  The most common suitable places are inside a `progn' or an
  812. implicit `progn' (*note Sequencing::.).
  813.  
  814. 
  815. File: lispref.info,  Node: Using Debugger,  Next: Debugger Commands,  Prev: Explicit Debug,  Up: Debugger
  816.  
  817. Using the Debugger
  818. ------------------
  819.  
  820.    When the debugger is entered, it displays the previously selected
  821. buffer in one window and a buffer named `*Backtrace*' in another
  822. window.  The backtrace buffer contains one line for each level of Lisp
  823. function execution currently going on.  At the beginning of this buffer
  824. is a message describing the reason that the debugger was invoked (such
  825. as the error message and associated data, if it was invoked due to an
  826. error).
  827.  
  828.    The backtrace buffer is read-only and uses a special major mode,
  829. Debugger mode, in which letters are defined as debugger commands.  The
  830. usual XEmacs editing commands are available; thus, you can switch
  831. windows to examine the buffer that was being edited at the time of the
  832. error, switch buffers, visit files, or do any other sort of editing.
  833. However, the debugger is a recursive editing level (*note Recursive
  834. Editing::.) and it is wise to go back to the backtrace buffer and exit
  835. the debugger (with the `q' command) when you are finished with it.
  836. Exiting the debugger gets out of the recursive edit and kills the
  837. backtrace buffer.
  838.  
  839.    The backtrace buffer shows you the functions that are executing and
  840. their argument values.  It also allows you to specify a stack frame by
  841. moving point to the line describing that frame.  (A stack frame is the
  842. place where the Lisp interpreter records information about a particular
  843. invocation of a function.)  The frame whose line point is on is
  844. considered the "current frame".  Some of the debugger commands operate
  845. on the current frame.
  846.  
  847.    The debugger itself must be run byte-compiled, since it makes
  848. assumptions about how many stack frames are used for the debugger
  849. itself.  These assumptions are false if the debugger is running
  850. interpreted.
  851.  
  852. 
  853. File: lispref.info,  Node: Debugger Commands,  Next: Invoking the Debugger,  Prev: Using Debugger,  Up: Debugger
  854.  
  855. Debugger Commands
  856. -----------------
  857.  
  858.    Inside the debugger (in Debugger mode), these special commands are
  859. available in addition to the usual cursor motion commands.  (Keep in
  860. mind that all the usual facilities of XEmacs, such as switching windows
  861. or buffers, are still available.)
  862.  
  863.    The most important use of debugger commands is for stepping through
  864. code, so that you can see how control flows.  The debugger can step
  865. through the control structures of an interpreted function, but cannot do
  866. so in a byte-compiled function.  If you would like to step through a
  867. byte-compiled function, replace it with an interpreted definition of the
  868. same function.  (To do this, visit the source file for the function and
  869. type `C-M-x' on its definition.)
  870.  
  871.    Here is a list of Debugger mode commands:
  872.  
  873. `c'
  874.      Exit the debugger and continue execution.  This resumes execution
  875.      of the program as if the debugger had never been entered (aside
  876.      from the effect of any variables or data structures you may have
  877.      changed while inside the debugger).
  878.  
  879.      Continuing when an error or quit was signalled will cause the
  880.      normal action of the signalling to take place.  If you do not want
  881.      this to happen, but instead want the program execution to continue
  882.      as if the call to `signal' did not occur, use the `r' command.
  883.  
  884. `d'
  885.      Continue execution, but enter the debugger the next time any Lisp
  886.      function is called.  This allows you to step through the
  887.      subexpressions of an expression, seeing what values the
  888.      subexpressions compute, and what else they do.
  889.  
  890.      The stack frame made for the function call which enters the
  891.      debugger in this way will be flagged automatically so that the
  892.      debugger will be called again when the frame is exited.  You can
  893.      use the `u' command to cancel this flag.
  894.  
  895. `b'
  896.      Flag the current frame so that the debugger will be entered when
  897.      the frame is exited.  Frames flagged in this way are marked with
  898.      stars in the backtrace buffer.
  899.  
  900. `u'
  901.      Don't enter the debugger when the current frame is exited.  This
  902.      cancels a `b' command on that frame.
  903.  
  904. `e'
  905.      Read a Lisp expression in the minibuffer, evaluate it, and print
  906.      the value in the echo area.  The debugger alters certain important
  907.      variables, and the current buffer, as part of its operation; `e'
  908.      temporarily restores their outside-the-debugger values so you can
  909.      examine them.  This makes the debugger more transparent.  By
  910.      contrast, `M-:' does nothing special in the debugger; it shows you
  911.      the variable values within the debugger.
  912.  
  913. `q'
  914.      Terminate the program being debugged; return to top-level XEmacs
  915.      command execution.
  916.  
  917.      If the debugger was entered due to a `C-g' but you really want to
  918.      quit, and not debug, use the `q' command.
  919.  
  920. `r'
  921.      Return a value from the debugger.  The value is computed by
  922.      reading an expression with the minibuffer and evaluating it.
  923.  
  924.      The `r' command is useful when the debugger was invoked due to exit
  925.      from a Lisp call frame (as requested with `b'); then the value
  926.      specified in the `r' command is used as the value of that frame.
  927.      It is also useful if you call `debug' and use its return value.
  928.  
  929.      If the debugger was entered at the beginning of a function call,
  930.      `r' has the same effect as `c', and the specified return value
  931.      does not matter.
  932.  
  933.      If the debugger was entered through a call to `signal' (i.e. as a
  934.      result of an error or quit), then returning a value will cause the
  935.      call to `signal' itself to return, rather than throwing to
  936.      top-level or invoking a handler, as is normal.  This allows you to
  937.      correct an error (e.g. the type of an argument was wrong) or
  938.      continue from a `debug-on-quit' as if it never happened.
  939.  
  940.      Note that some errors (e.g. any error signalled using the `error'
  941.      function, and many errors signalled from a primitive function) are
  942.      not continuable.  If you return a value from them and continue
  943.      execution, then the error will immediately be signalled again.
  944.      Other errors (e.g. wrong-type-argument errors) will be continually
  945.      resignalled until the problem is corrected.
  946.  
  947. 
  948. File: lispref.info,  Node: Invoking the Debugger,  Next: Internals of Debugger,  Prev: Debugger Commands,  Up: Debugger
  949.  
  950. Invoking the Debugger
  951. ---------------------
  952.  
  953.    Here we describe fully the function used to invoke the debugger.
  954.  
  955.  - Function: debug &rest DEBUGGER-ARGS
  956.      This function enters the debugger.  It switches buffers to a buffer
  957.      named `*Backtrace*' (or `*Backtrace*<2>' if it is the second
  958.      recursive entry to the debugger, etc.), and fills it with
  959.      information about the stack of Lisp function calls.  It then
  960.      enters a recursive edit, showing the backtrace buffer in Debugger
  961.      mode.
  962.  
  963.      The Debugger mode `c' and `r' commands exit the recursive edit;
  964.      then `debug' switches back to the previous buffer and returns to
  965.      whatever called `debug'.  This is the only way the function
  966.      `debug' can return to its caller.
  967.  
  968.      If the first of the DEBUGGER-ARGS passed to `debug' is `nil' (or
  969.      if it is not one of the special values in the table below), then
  970.      `debug' displays the rest of its arguments at the top of the
  971.      `*Backtrace*' buffer.  This mechanism is used to display a message
  972.      to the user.
  973.  
  974.      However, if the first argument passed to `debug' is one of the
  975.      following special values, then it has special significance.
  976.      Normally, these values are passed to `debug' only by the internals
  977.      of XEmacs and the debugger, and not by programmers calling `debug'.
  978.  
  979.      The special values are:
  980.  
  981.     `lambda'
  982.           A first argument of `lambda' means `debug' was called because
  983.           of entry to a function when `debug-on-next-call' was
  984.           non-`nil'.  The debugger displays `Entering:' as a line of
  985.           text at the top of the buffer.
  986.  
  987.     `debug'
  988.           `debug' as first argument indicates a call to `debug' because
  989.           of entry to a function that was set to debug on entry.  The
  990.           debugger displays `Entering:', just as in the `lambda' case.
  991.           It also marks the stack frame for that function so that it
  992.           will invoke the debugger when exited.
  993.  
  994.     `t'
  995.           When the first argument is `t', this indicates a call to
  996.           `debug' due to evaluation of a list form when
  997.           `debug-on-next-call' is non-`nil'.  The debugger displays the
  998.           following as the top line in the buffer:
  999.  
  1000.                Beginning evaluation of function call form:
  1001.  
  1002.     `exit'
  1003.           When the first argument is `exit', it indicates the exit of a
  1004.           stack frame previously marked to invoke the debugger on exit.
  1005.           The second argument given to `debug' in this case is the
  1006.           value being returned from the frame.  The debugger displays
  1007.           `Return value:' on the top line of the buffer, followed by
  1008.           the value being returned.
  1009.  
  1010.     `error'
  1011.           When the first argument is `error', the debugger indicates
  1012.           that it is being entered because an error or `quit' was
  1013.           signaled and not handled, by displaying `Signaling:' followed
  1014.           by the error signaled and any arguments to `signal'.  For
  1015.           example,
  1016.  
  1017.                (let ((debug-on-error t))
  1018.                  (/ 1 0))
  1019.                
  1020.                ------ Buffer: *Backtrace* ------
  1021.                Signaling: (arith-error)
  1022.                  /(1 0)
  1023.                ...
  1024.                ------ Buffer: *Backtrace* ------
  1025.  
  1026.           If an error was signaled, presumably the variable
  1027.           `debug-on-error' is non-`nil'.  If `quit' was signaled, then
  1028.           presumably the variable `debug-on-quit' is non-`nil'.
  1029.  
  1030.     `nil'
  1031.           Use `nil' as the first of the DEBUGGER-ARGS when you want to
  1032.           enter the debugger explicitly.  The rest of the DEBUGGER-ARGS
  1033.           are printed on the top line of the buffer.  You can use this
  1034.           feature to display messages--for example, to remind yourself
  1035.           of the conditions under which `debug' is called.
  1036.  
  1037. 
  1038. File: lispref.info,  Node: Internals of Debugger,  Prev: Invoking the Debugger,  Up: Debugger
  1039.  
  1040. Internals of the Debugger
  1041. -------------------------
  1042.  
  1043.    This section describes functions and variables used internally by the
  1044. debugger.
  1045.  
  1046.  - Variable: debugger
  1047.      The value of this variable is the function to call to invoke the
  1048.      debugger.  Its value must be a function of any number of arguments
  1049.      (or, more typically, the name of a function).  Presumably this
  1050.      function will enter some kind of debugger.  The default value of
  1051.      the variable is `debug'.
  1052.  
  1053.      The first argument that Lisp hands to the function indicates why it
  1054.      was called.  The convention for arguments is detailed in the
  1055.      description of `debug'.
  1056.  
  1057.  - Command: backtrace
  1058.      This function prints a trace of Lisp function calls currently
  1059.      active.  This is the function used by `debug' to fill up the
  1060.      `*Backtrace*' buffer.  It is written in C, since it must have
  1061.      access to the stack to determine which function calls are active.
  1062.      The return value is always `nil'.
  1063.  
  1064.      In the following example, a Lisp expression calls `backtrace'
  1065.      explicitly.  This prints the backtrace to the stream
  1066.      `standard-output': in this case, to the buffer `backtrace-output'.
  1067.      Each line of the backtrace represents one function call.  The
  1068.      line shows the values of the function's arguments if they are all
  1069.      known.  If they are still being computed, the line says so.  The
  1070.      arguments of special forms are elided.
  1071.  
  1072.           (with-output-to-temp-buffer "backtrace-output"
  1073.             (let ((var 1))
  1074.               (save-excursion
  1075.                 (setq var (eval '(progn
  1076.                                    (1+ var)
  1077.                                    (list 'testing (backtrace))))))))
  1078.           
  1079.                => nil
  1080.  
  1081.           ----------- Buffer: backtrace-output ------------
  1082.             backtrace()
  1083.             (list ...computing arguments...)
  1084.             (progn ...)
  1085.             eval((progn (1+ var) (list (quote testing) (backtrace))))
  1086.             (setq ...)
  1087.             (save-excursion ...)
  1088.             (let ...)
  1089.             (with-output-to-temp-buffer ...)
  1090.             eval-region(1973 2142 #<buffer *scratch*>)
  1091.             byte-code("...  for eval-print-last-sexp ...")
  1092.             eval-print-last-sexp(nil)
  1093.           * call-interactively(eval-print-last-sexp)
  1094.           ----------- Buffer: backtrace-output ------------
  1095.  
  1096.      The character `*' indicates a frame whose debug-on-exit flag is
  1097.      set.
  1098.  
  1099.  - Variable: debug-on-next-call
  1100.      If this variable is non-`nil', it says to call the debugger before
  1101.      the next `eval', `apply' or `funcall'.  Entering the debugger sets
  1102.      `debug-on-next-call' to `nil'.
  1103.  
  1104.      The `d' command in the debugger works by setting this variable.
  1105.  
  1106.  - Function: backtrace-debug LEVEL FLAG
  1107.      This function sets the debug-on-exit flag of the stack frame LEVEL
  1108.      levels down the stack, giving it the value FLAG.  If FLAG is
  1109.      non-`nil', this will cause the debugger to be entered when that
  1110.      frame later exits.  Even a nonlocal exit through that frame will
  1111.      enter the debugger.
  1112.  
  1113.      This function is used only by the debugger.
  1114.  
  1115.  - Variable: command-debug-status
  1116.      This variable records the debugging status of the current
  1117.      interactive command.  Each time a command is called interactively,
  1118.      this variable is bound to `nil'.  The debugger can set this
  1119.      variable to leave information for future debugger invocations
  1120.      during the same command.
  1121.  
  1122.      The advantage, for the debugger, of using this variable rather than
  1123.      another global variable is that the data will never carry over to a
  1124.      subsequent command invocation.
  1125.  
  1126.  - Function: backtrace-frame FRAME-NUMBER
  1127.      The function `backtrace-frame' is intended for use in Lisp
  1128.      debuggers.  It returns information about what computation is
  1129.      happening in the stack frame FRAME-NUMBER levels down.
  1130.  
  1131.      If that frame has not evaluated the arguments yet (or is a special
  1132.      form), the value is `(nil FUNCTION ARG-FORMS...)'.
  1133.  
  1134.      If that frame has evaluated its arguments and called its function
  1135.      already, the value is `(t FUNCTION ARG-VALUES...)'.
  1136.  
  1137.      In the return value, FUNCTION is whatever was supplied as the CAR
  1138.      of the evaluated list, or a `lambda' expression in the case of a
  1139.      macro call.  If the function has a `&rest' argument, that is
  1140.      represented as the tail of the list ARG-VALUES.
  1141.  
  1142.      If FRAME-NUMBER is out of range, `backtrace-frame' returns `nil'.
  1143.  
  1144. 
  1145. File: lispref.info,  Node: Syntax Errors,  Next: Compilation Errors,  Prev: Debugger,  Up: Debugging
  1146.  
  1147. Debugging Invalid Lisp Syntax
  1148. =============================
  1149.  
  1150.    The Lisp reader reports invalid syntax, but cannot say where the real
  1151. problem is.  For example, the error "End of file during parsing" in
  1152. evaluating an expression indicates an excess of open parentheses (or
  1153. square brackets).  The reader detects this imbalance at the end of the
  1154. file, but it cannot figure out where the close parenthesis should have
  1155. been.  Likewise, "Invalid read syntax: ")"" indicates an excess close
  1156. parenthesis or missing open parenthesis, but does not say where the
  1157. missing parenthesis belongs.  How, then, to find what to change?
  1158.  
  1159.    If the problem is not simply an imbalance of parentheses, a useful
  1160. technique is to try `C-M-e' at the beginning of each defun, and see if
  1161. it goes to the place where that defun appears to end.  If it does not,
  1162. there is a problem in that defun.
  1163.  
  1164.    However, unmatched parentheses are the most common syntax errors in
  1165. Lisp, and we can give further advice for those cases.
  1166.  
  1167. * Menu:
  1168.  
  1169. * Excess Open::     How to find a spurious open paren or missing close.
  1170. * Excess Close::    How to find a spurious close paren or missing open.
  1171.  
  1172.